package com.sojson.service.pay.wx.impl.online;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import com.ijpay.core.IJPayHttpResponse;
import com.ijpay.core.kit.WxPayKit;
import com.sojson.config.condition.OnlineConditionPayWx;
import com.sojson.config.exception.TransException;
import com.sojson.service.pay.wx.PayWxParamNofityVerifyInterface;
import com.sojson.util.StringUtil;
import com.sojson.util.SystemParamUtil;
import com.sojson.util.json.JsonUtil;
import com.sojson.util.pay.wx.bean.WxResponseV3;
import com.sojson.util.pay.wx.constant.WxConstant;
import com.sojson.util.servlet.ServletUtil;

/**
 * 微信支付回调参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
@Component
/** 是否注入这个Bean */
@Conditional(OnlineConditionPayWx.class)
public class PayWxParamNofityVerify implements PayWxParamNofityVerifyInterface {

    /**
     * 支付回调
     * 
     * @param request 
     * @return
     * @throws Exception 
     */
    @Override
    public WxResponseV3 payNofity(HttpServletRequest request) throws Exception {
        // 响应证书序列号
        String serial = request.getHeader("Wechatpay-Serial");
        // 响应时间戳
        String timestamp = request.getHeader("Wechatpay-Timestamp");
        // 响应随机字符串
        String nonce = request.getHeader("Wechatpay-Nonce");
        // 响应主体
        String signature = request.getHeader("Wechatpay-Signature");

        if (StringUtil.isBlank(signature)) {
            throw new TransException("应答证书序列号为空！");
        }
        if (StringUtil.isBlank(timestamp)) {
            throw new TransException("应答时间戳为空！");
        }
        if (StringUtil.isBlank(nonce)) {
            throw new TransException("应答随机串为空！");
        }
        if (StringUtil.isBlankObject(signature)) {
            throw new TransException("应答报文主体为空！");
        }

        String stringByRequest = ServletUtil.getParamBody();
        HashMap<String, List<String>> map = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement();
            Enumeration<String> headers = request.getHeaders(headName);
            List<String> list = new ArrayList<>();
            while (headers.hasMoreElements()) {
                list.add(headers.nextElement());
            }
            map.put(headName, list);
        }
        IJPayHttpResponse ijPayHttpResponse = new IJPayHttpResponse();
        ijPayHttpResponse.setBody(stringByRequest);
        ijPayHttpResponse.setHeaders(map);

        // 验证签名
        String verifyNotify = WxPayKit.verifyNotify(serial, stringByRequest, signature, nonce, timestamp,
            SystemParamUtil.get(WxConstant.APIV3KEY_CACHE_NAME), WxConstant.CA_CERT_FILE_PATH_PEM_WECHATPAY);

        return JsonUtil.jsonToObject(verifyNotify, WxResponseV3.class);
    }

}